using Services;
using System.Collections;
using UnityEngine;
using UnityEngine.EventSystems;

public class ItemUI_OptionPanel : UI_Dynamic, IPointerClickHandler
{
    private ItemUI_Manager manager;
    private ItemUI_Option buttonManager;
    public Player Player { get; private set; }

    protected override void Awake()
    {
        base.Awake();
        manager = CommonUI.GetUIComponent<ItemUI_Manager>();
        buttonManager = GetComponentInChildren<ItemUI_Option>();
        rectTransform = buttonManager.GetComponent<RectTransform>();
        m_transform = buttonManager.transform;
        ServiceLocator.Get<Services.EventSystem>().AddListener<EGameState, EGameState>(EEvent.GameStateChange, OnGameStateChange);
        manager.RightClickItem += OnRightClickItem;
    }

    private void Start()
    {
        Player = ServiceLocator.Get<ActionController>().player;
    }

    private void OnRightClickItem()
    {
        if (manager.TargetStack.ItemAttached.b_cannotUse)
            return;
        StartCoroutine(DelayMove());
    }
    private IEnumerator DelayMove()
    {
        yield return null;
        Width = rectTransform.rect.width / 800 * Screen.width;
        Height = rectTransform.rect.height / 450 * Screen.height;
        Move(Input.mousePosition);
        Visible = true;
    }
    public void Hide()
    {
        Visible = false;
    }

    public void OnPointerClick(PointerEventData eventData)
    {
        Hide();
    }

    private void OnGameStateChange(EGameState from, EGameState to)
    {
        Hide();
    }
}
